﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using System.Diagnostics;
using NAudio.CoreAudioApi;
using System.Collections.ObjectModel;

using System.IO;



namespace Naudio_playback_completed
{
    public partial class Form1 : Form
    {
        private NAudio.Wave.WaveFileReader wfr = null;
        
        
          private NAudio.Wave.DirectSoundOut audioOutput = null;
      
     //   private NAudio.Wave.WaveOut audioOutput = null; 


        public Form1()
        {
            InitializeComponent();
        }

        private void buttonPlay_Click(object sender, EventArgs e)
        {
            wfr = new NAudio.Wave.WaveFileReader("001.wav");
            audioOutput = new DirectSoundOut(100);  // new WaveOut();


            WaveChannel32 wc = new WaveChannel32(wfr);
            wc.PadWithZeroes = false;
            audioOutput.Init(wc);
           
            audioOutput.PlaybackStopped += new EventHandler<StoppedEventArgs>(audioOutput_PlaybackStopped);
            audioOutput.Play();
           

        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (audioOutput != null)
            {
                audioOutput.Stop();

            }
        }

        void audioOutput_PlaybackStopped(object sender, StoppedEventArgs e)
        {
            Console.WriteLine("Stop!");
            if (audioOutput != null)
            {
                audioOutput.Stop();
                audioOutput.Dispose();
                audioOutput = null;
            }

            if (wfr != null)
            {
                wfr.Close();
                wfr.Dispose();
                wfr = null;
            }
        }
    }
}
